//
//  HBaseViewController.m
//  SSIL
//
//  Created by 余胜民 on 16/5/11.
//  Copyright © 2016年 sanmy. All rights reserved.
//

#import "HBaseViewController.h"

//导航字体颜色
#define BaseNaviBarTextColor            [UIColor baseTextOrangeColor]
#define BaseNaviBarTextHighLightColor   [UIColor baseTextHighLightOrangeColor]
#define BaseNaviBarTextDisableColor     [UIColor baseTextGrayColor]

@interface HBaseViewController ()

@property (nonatomic,strong) UITapGestureRecognizer * dimissKeyBoardGesture;

@end

@implementation HBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    /**
     *  设置默认背景颜色
     */
    self.view.backgroundColor = [UIColor baseGrayBackgroundColor];
    
    /**
     *  设置默认导航返回按钮
     */
    [self setLeftBackBarButton];
    
    
    /**
     *  设置基础布局
     */
    [self setupRootView];
    
    /**
     *  设置导航透明度和边界
     */
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor baseNaviBackGroundColor];
    
    /**
     *  添加软键盘监听－区域外关闭
     */
    [self registerKeyboard];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 基础功能事件

/**
 *  返回上一级页面
 */
- (void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  基础布局函数，父类默认在viewDidLoad中调用，子类在没有特殊情况的时候应该在其中布局
 */

- (void)setupRootView{
    
}

/**
 *  注册软键盘监听
 */
- (void)registerKeyboard{
    _dimissKeyBoardGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
    _dimissKeyBoardGesture.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:_dimissKeyBoardGesture];
}

/**
 *  移除软键盘监听
 */
- (void)removeKeyBoardObserver{
    if (_dimissKeyBoardGesture) {
        [self.view removeGestureRecognizer:_dimissKeyBoardGesture];
    }
}

/**
 *  关闭软键盘
 *
 *  @param sender 返回手势
 */
-(void)dismissKeyboard:(id)sender{
    [self.view endEditing:YES];
}

#pragma mark 导航组建
/**
 *  设置导航默认返回按钮
 */
- (void)setLeftBackBarButton{
    
    _leftBarButton = [[UIButton alloc] init];
//    [_leftBarButton setTitle:@"返回" forState:UIControlStateNormal];
//    [_leftBarButton setTitleColor:BaseNaviBarTextColor forState:UIControlStateNormal];
//    [_leftBarButton setTitleColor:BaseNaviBarTextHighLightColor forState:UIControlStateHighlighted];
    [_leftBarButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    //[_leftBarButton setImage:[UIImage imageNamed:@"nav_back_orange_pressed"] forState:UIControlStateHighlighted];
    //_leftBarButton.titleLabel.font = [UIFont menuFont];
    _leftBarButton.frame = CGRectMake(0, 0, 50, 44);
    _leftBarButton.imageEdgeInsets = UIEdgeInsetsMake(0, -6, 0, 6);
    [_leftBarButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -4;
    
    UIBarButtonItem *leftButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:_leftBarButton];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,leftButtonItem, nil];
}
/**
 *  设置导航左侧按钮图片
 *
 *  @param normalImage    正常图片
 *  @param highLightImage 高亮图片
 */
- (void)setLeftBarButtonWithImage:(UIImage *)normalImage AndHighLightImage:(UIImage *)highLightImage{
    
    _leftBarButton                        = [[UIButton alloc] init];
    _leftBarButton.frame                  = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
    [_leftBarButton setImage:normalImage forState:UIControlStateNormal];
    [_leftBarButton setImage:highLightImage forState:UIControlStateHighlighted];
    UIBarButtonItem *leftButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:_leftBarButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -4;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,leftButtonItem, nil];
}

/**
 *  设置导航左侧按钮文本
 *
 *  @param text 导航按钮文本
 */
- (void)setLeftBarButtonWithText:(NSString *)text{
    
    CGSize buttonSize                     = [text getSizeWithFont:[UIFont menuFont]];
    _leftBarButton                        = [[UIButton alloc] init];
    _leftBarButton.frame                  = CGRectMake(0, 0, buttonSize.width, buttonSize.height);
    _leftBarButton.titleLabel.font        = [UIFont menuFont];
    [_leftBarButton setTitle:text forState:UIControlStateNormal];
    [_leftBarButton setTitle:text forState:UIControlStateHighlighted];
    [_leftBarButton setTitleColor:BaseNaviBarTextColor forState:UIControlStateNormal];
    [_leftBarButton setTitleColor:BaseNaviBarTextHighLightColor forState:UIControlStateHighlighted];
    UIBarButtonItem *leftButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:_leftBarButton];
    
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -4;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,leftButtonItem, nil];
}

/**
 *  设置导航右侧按钮图片
 *
 *  @param normalImage    正常图片
 *  @param highLightImage 高亮图片
 */
- (void)setRightBarButtonWithImage:(UIImage *)normalImage AndHighLightImage:(UIImage *)highLightImage{
    
    _rightBarButton                        = [[UIButton alloc] init];
    _rightBarButton.frame                  = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
    [_rightBarButton setImage:normalImage forState:UIControlStateNormal];
    [_rightBarButton setImage:highLightImage forState:UIControlStateHighlighted];
    UIBarButtonItem *rightButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:_rightBarButton];
    
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -4;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:space,rightButtonItem, nil];
}
- (void)setLeftBarButtonWithImage:(UIImage *)normalImage AndHighLightImage:(UIImage *)highLightImage AndText:(NSString *)text{
    CGSize textSize = [text getSizeWithFont:[UIFont baseFont]];
    CGFloat height = normalImage.size.height > textSize.height ? normalImage.size.height : textSize.height;
    CGFloat width = textSize.width + normalImage.size.width + 8;
    _leftBarButton                        = [[UIButton alloc] init];
    _leftBarButton.frame                  = CGRectMake(0, 0, width, height);
    _leftBarButton.titleLabel.font        = [UIFont baseFont];
    [_leftBarButton setImage:normalImage forState:UIControlStateNormal];
    [_leftBarButton setImage:highLightImage forState:UIControlStateHighlighted];
    [_leftBarButton setTitle:text forState:UIControlStateNormal];
    [_leftBarButton setTitle:text forState:UIControlStateHighlighted];
    [_leftBarButton setTitleColor:BaseNaviBarTextColor forState:UIControlStateNormal];
    [_leftBarButton setTitleColor:BaseNaviBarTextColor forState:UIControlStateHighlighted];
    [_leftBarButton setTitleColor:BaseNaviBarTextDisableColor forState:UIControlStateDisabled];
    _leftBarButton.imageEdgeInsets = UIEdgeInsetsMake(0, -6, 0, 6);
    UIBarButtonItem *leftButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:_leftBarButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -4;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,leftButtonItem, nil];
}

/**
 *  设置导航右侧按钮文本
 *
 *  @param text 导航按钮文本
 */
- (void)setRightBarButtonWithText:(NSString *)text{
    
    CGSize buttonSize                      = [text getSizeWithFont:[UIFont menuFont]];
    _rightBarButton                        = [[UIButton alloc] init];
    _rightBarButton.frame                  = CGRectMake(0, 0, buttonSize.width, buttonSize.height);
    _rightBarButton.titleLabel.font        = [UIFont menuFont];
    [_rightBarButton setTitle:text forState:UIControlStateNormal];
    [_rightBarButton setTitle:text forState:UIControlStateHighlighted];
    [_rightBarButton setTitleColor:BaseNaviBarTextColor forState:UIControlStateNormal];
    [_rightBarButton setTitleColor:BaseNaviBarTextHighLightColor forState:UIControlStateHighlighted];
    [_rightBarButton setTitleColor:BaseNaviBarTextDisableColor forState:UIControlStateDisabled];
    UIBarButtonItem *rightButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:_rightBarButton];
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -4;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:space,rightButtonItem, nil];
}

/**
 *  添加或者移除加载动画
 *
 *  @param isNeedLoadingView 是否需要加载动画
 */
- (void)setIsNeedLoadingView:(BOOL)isNeedLoadingView{
    if (isNeedLoadingView) {
        if(!_loadingView){
            _loadingView = [[LoadingView alloc] init];
        }
        [self.view addSubview:_loadingView];
    }else{
        if (_loadingView) {
            [_loadingView removeFromSuperview];
        }
    }
}
/**
 *  显示默认加载动画
 */
- (void)showLoadingView{
    [self setIsNeedLoadingView:YES];
    if ([_loadingView isAnimating]) {
        return;
    }
    [self.view bringSubviewToFront:_loadingView];
    [_loadingView startAnimating];
}
/**
 *  显示加载动画 并修改加载文案
 *
 *  @param text 文案
 */
- (void)showLoadingViewWithText:(NSString *)text{
    [self setIsNeedLoadingView:YES];
    if (text) {
        _loadingView.contentText = text;
    }
    if ([_loadingView isAnimating]) {
        return;
    }
    [self.view bringSubviewToFront:_loadingView];
    [_loadingView startAnimating];
}

/**
 *  关闭加载动画
 */
- (void)dismissLoadingView{
    if ([_loadingView isAnimating]) {
        [_loadingView stopAnimating];
    }
    [self.view sendSubviewToBack:_loadingView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
